import {
  getProfessionHome,
  getLiveAdsAndHotAcs,
  getDownloadProfession
} from '@/api/profession_home'
import { getInformations } from '@/api/information_home'
import { getAllNavs } from '@/api/common_apis'
import tool from '@/utils/tool'
export default {
  namespaced: true,
  state: () => ({
    // 导航左侧数据
    left_column: {},
    // 顶部banner
    top_banners: [],
    // 轮播banner
    loop_banners: [],
    // 中部banner
    middle_banners: [],
    // 免费看课下方banner
    free_course_banners: [],
    // 头条资讯
    headlines: [],
    // 公开课数据
    public_lives: [],
    // 免费课
    free_courses: {},
    // 考试时间
    exam_timeline: [],
    // 热门资讯
    hot_news: [],
    //系统课程
    system_course: [],
    // 之了名师
    teachers: [],
    // 配套图书
    book: [],
    // 资讯
    news: [],
    // 热点关注
    hot_focus: [],
    // 学习专区
    study_area: {},
    // 当前的试听url
    curVideoUrl: '',
    // 专业id
    profession_id: '',
    // 直播右侧、热门活动广告数据
    liveAds: [],
    hotAds: [],
    // 所有专业
    professions: [],
    // 所有下载栏目
    downloadNavs: [],
    //下载资料专业
    downloadProfession: [],
    // 获取最新资讯
    newInfoList: [],
    // 热门推荐
    hot_activity: []
  }),
  mutations: {
    // 设置专业首页数据
    setProfessionHome(state, data) {
      // console.log(data.hot_activit);
      // 处理热门活动
      state.hot_activity = Array.isArray(data.hot_activity)
        ? data.hot_activity
        : []
      // 处理系统课程数据
      data.system_course =
        (Array.isArray(data.system_course) && data.system_course.slice(0, 4)) ||
        []
      // 处理系统课程
      data.system_course.forEach((itemB, indexB) => {
        itemB.right_icon = `//image.zlketang.com/public/news/images/new_official/home/label${indexB +
          1}.png`

        itemB.description =
          typeof itemB.description === 'string'
            ? itemB.description.split('\n').slice(0, 3)
            : []
      })
      // 处理配套书籍
      // 处理图书信息
      Array.isArray(data.book) &&
        data.book.forEach((itemB, indexB) => {
          Array.isArray(itemB.show_detail) &&
            itemB.show_detail.forEach((itemC, indexC) => {
              let { description } = itemC
              itemC.book_desc = []
              itemC.book_privilege = []
              if (Array.isArray(description)) {
                itemC.book_desc = description[0]
                  ? description[0].split(/\n/)
                  : []
                itemC.book_privilege = description[1]
                  ? description[1].split(/\n/)
                  : []
              }
            })
        })
      // 不能解构state
      let propertyType = [
        'left_column',
        'top_banners',
        'loop_banners',
        'middle_banners',
        'free_course_banners',
        'headlines',
        'public_lives',
        'free_courses',
        'exam_timeline',
        'hot_news',
        'system_course',
        'teachers',
        'book',
        'news',
        'hot_focus',
        'study_area',
        'download_nav'
      ]
      propertyType.forEach(item => {
        state[item] = data[item] || []
      })
      state['study_area'] =
        data['study_area'] && Object.keys(data['study_area']).length
          ? data['study_area']
          : { 1: [], 2: [], 3: [], 4: [] }
      data['left_column'] =
        data['left_column'] && Object.keys(data['left_column']).length
          ? data['left_column']
          : { 1: [], 2: [], 3: [] }
      data['left_column'][2] = tool.handleJumpData(data['left_column'][2])
      data['left_column'][3] = tool.handleJumpData(data['left_column'][3])
      state['left_column'] = data['left_column']
      state['free_courses'] =
        data['free_courses'] && Object.keys(data['free_courses']).length
          ? data['free_courses']
          : { 1: [], 2: [], 3: [] }
      state.curVideoUrl =
        data.free_courses && data.free_courses[1] && data.free_courses[1][0]
          ? data.free_courses[1][0].jump_extend.jump_url
          : ''
      if (state['public_lives'] && Array.isArray(state['public_lives'])) {
        state['public_lives'].forEach(item => {
          item.play_status_old = item.play_status
        })
      }
    },
    // 设置专业id
    setProfessionId(state, data) {
      state.profession_id = data
    },
    // 设置热门活动和直播右侧广告
    setLiveAdsAndHotAds(state, { live_ads = [], hot_ads = [] }) {
      state.liveAds = live_ads
      state.hotAds = hot_ads
    },
    // 设置所有专业、对应的资料下载栏目
    setProfessionAndDownloadNavs(state, { professionList, navList }) {
      state.professions = tool.encodeString(professionList)

      // state.professions = professionList
      state.downLoadNavs = tool.encodeString(
        navList.filter(item => +item.parent_id === 4)
      )
      // state.downLoadNavs = navList.filter(item => +item.parent_id === 4)
    },
    //设置下载时的专业
    setDownloadProfession(state, data) {
      // state.downloadProfession = data
      state.downloadProfession = tool.encodeString(data)
    },
    // 设置最新资讯
    setNewInformations(state, data) {
      // state.newInfoList = data
      // console.log(encodeURI(JSON.stringify(data)),'data');
      state.newInfoList = tool.encodeString(data)
    }
  },
  actions: {
    // 获取专业首页数据
    async getProfessionHome({ commit, state, rootState }, params) {
      commit('setProfessionId', params.profession_id)
      let res = await getProfessionHome(params).catch(err => {
        return { data: {} }
      })
      // 设置顶部导航栏
      let arr = (res.data && res.data.menu) || []
      rootState.navList = tool.encodeString(arr)
      // rootState.navList = (res.data && res.data.menu) || []
      rootState.activity_banner =
        (res.data &&
          res.data.top_banners &&
          tool.encodeString(res.data.top_banners[0])) ||
        {}
      res && res.data && commit('setProfessionHome', res.data)
    },
    //获取直播右侧广告+热门活动广告
    async getLiveAdsAndHotAcs({ commit }, params) {
      let res = await getLiveAdsAndHotAcs(params).catch(err => {
        return {
          data: {}
        }
      })
      if (res.data) {
        commit('setLiveAdsAndHotAds', res.data)
      }
    },

    //获取下载资料专业
    async getDownloadProfession({ commit, state, rootState }, params) {
      commit('setProfessionId', params.profession_id)
      let res = await getDownloadProfession(params).catch(err => {
        return { data: {} }
      })
      if (res.data) {
        commit('setDownloadProfession', res.data)
      }
    },
    // 获取最新资讯
    async getInformations({ commit, state, rootState }, params) {
      // console.log(params);
      let res = await getInformations(params).catch(err => {
        return { data: {} }
      })
      if (res.data.data) {
        let arr = res.data.data || []
        let newInfoList = []
        arr.map(item => {
          if (newInfoList.length < 6 && item.headimg) {
            newInfoList.push(item)
          }
        })
        commit('setNewInformations', newInfoList)
      }
    }
  },
  getters: {}
}
